import { ref } from 'vue'
import { departmentDelete } from '@/api/system'
import msg from '@/utils/resetMessage'
/**
 * table的新增、编辑、删除逻辑（函数入参）
 *    getTableData：获取列表函数
 * */
export default function (getTableData: Function) {
  const modalRef = ref()
  // 新增
  const show = ref<boolean>(false)
  const title = ref<string>('')
  function addBtn() {
    show.value = true
    title.value = '新增'
  }

  // 编辑
  function editBtn(item: any) {
    show.value = true
    title.value = '编辑'
  }

  // 新增/编辑的确定按钮
  function sure() {
    show.value = false
    getTableData()
  }

  // 删除
  function deleteBtn(id: number) {
    departmentDelete(id)
      .then((res: any) => {
        if (res.code === 500) {
          msg.warning(res.msg)
          return
        }
        msg.success('删除成功')
        getTableData()
      })
      .catch(() => {})
  }

  return { modalRef, title, show, addBtn, editBtn, sure, deleteBtn }
}
